/*
 * @(#)OIDUtils.java
 * Date 2013-08-17
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.utils;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static us.hornerscorners.lamppost.utils.UUIDUtils.isValid;


/**
 *
 * @author jim
 */
public class OIDUtils {

    /** Field description */
    public static final String URN_PREFIX = "urn:oid:";

    /** Field description */
    private static final Logger logger = LoggerFactory.getLogger(OIDUtils.class);

    /**
     * Method description
     *
     *
     * @param oidstr
     *
     * @return
     */
    public static String createURN(String oidstr) {

        return String.format("%s%s", URN_PREFIX, oidstr);
    }

    /**
     * Method description
     *
     *
     * @param oid
     *
     * @return
     */
    public static boolean isNotValid(String oid) {

        return (isValid(oid) == false);
    }

    /**
     * Method description
     *
     *
     * @param str
     *
     * @return
     */
    public static boolean isNotValidWithURN(String str) {

        return (isValidWithURN(str) == false);
    }

    /**
     * Method description
     *
     *
     * @param oid
     *
     * @return
     */
    public static boolean isValid(String oid) {

        boolean foundError = false;

        String[] pieces = StringUtils.splitPreserveAllTokens(oid, ".");

        if ((pieces != null) && (pieces.length > 1)) {

            for (String piece : pieces) {

                if (StringUtils.isBlank(piece)
                        || (StringUtils.isNumeric(piece) == false)) {

                    logger.warn("OID Piece [{}] is invalid.", piece);
                    foundError = true;

                    break;
                }
            }
        }

        return (foundError == false);
    }

    /**
     * Method description
     *
     *
     *
     * @return
     */
    public static boolean isValidWithURN(String str) {

        boolean result = false;

        if (StringUtils.startsWith(str, URN_PREFIX)) {

            isValid(str);
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param uuidstr
     *
     * @return
     */
    public static String removeURNPrefix(String uuidstr) {

        return StringUtils.remove(uuidstr, URN_PREFIX);
    }
}
